package com.sofwin.controller;

import com.sofwin.Constants;
import com.sofwin.pojo.SysResource;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.ResourceService;
import com.sofwin.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 登录
 */
@Controller
public class LoginController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    UserService service;
    private Integer checks=0;
    /**
     * @Description:跳转首页
     * @Author: Mr.jhx
     * @Date: 2021/4/24 13:49
     * @Param: * @param session:
     * @param model:
     * @Return: * @return: java.lang.String
     **/


    @RequestMapping("/toIndex")
    public String test2(HttpSession session, Model model){
        SysUser user = (SysUser)session.getAttribute(Constants.CURR_USER);
        List<SysResource> resources = resourceService.selectAllResources();
        model.addAttribute("menus",resources);
        return "index";
    }


   /**
    * @Description: 判断用户是否存在，密码是否正确
    * @Author: Mr.jhx
    * @Date: 2021/4/24 13:48
    * @Param: * @param user:
    * @param check:
    * @param session:
    * @param model:
    * @Return: * @return: java.lang.String
    **/
    @RequestMapping("login")
    public String login(SysUser user, @RequestParam(defaultValue ="0") Integer check, HttpSession session , Model model){

//        if(code==null){
//            return "redirect:login.jsp";
//        }
//        String oldCode = session.g
//        if (!code.equals(oldCode)){
//            return "redirect:login.jsp";
//        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginAccount(),user.getLoginPwd());
        try {
            subject.login(token);
        }catch (UnknownAccountException ex){
            //用户名不存在
            return "redirect:login.jsp";
        }catch (IncorrectCredentialsException ex){
            //密码不正确
            return "redirect:login.jsp";
        }
            user = service.selectUser(user);
            session.setAttribute(Constants.CURR_USER,user);
            checks = check;

            return "redirect:toIndex";
    }
    /**
     * @Description:
     * @Author: Mr.jhx
     * @Date: 2021/4/24 13:50
     * @Param: * @param session:
     * @Return: * @return: java.lang.String
     **/
    @RequestMapping("logout")
    public String logout(HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        session.removeAttribute(Constants.CURR_USER);
        return "redirect:index.jsp";
    }

    @RequestMapping("welcome")
    public String welcome(){
        return "welcome";
    }

}


